﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;
using System.IO;
using FileManager;
public partial class fileUpdate_RecoverFile : System.Web.UI.Page
{
    public string userid = "";
    protected void Page_Load(object sender, EventArgs e)
    {
        TUser1.CheckUser(this);
        if (!TUser1.CheckGrant("A02", Session["userid"].ToString(), this))
        {
            Response.Write("<font style=\"FONT-SIZE: 10pt; COLOR: red\">对不起，您没有获得该项功能的授权！</font>");
            Response.End();
        }
        userid = Session["userid"].ToString();
        if (!IsPostBack)
        {
            BindGrid();
            string bigname = Session["username"].ToString();

            userstatus1.Text = "<a onclick='javascript:location.href=\"../user/user_myself.aspx\"' onmouseover='this.style.cursor=\"pointer\"' onmouseout='this.style.backgroundColor=\"\"'>" + bigname + "的设置</a>";
            userstatus2.Text = "<a onclick='javascript:location.href=\"../user/user_logout.aspx\"' onmouseover='this.style.cursor=\"pointer\"' onmouseout='this.style.backgroundColor=\"\"'>退出</a>";
        }
    }

    #region BindGrid()
    private void BindGrid()
    {
        List<FileSystemItem> list = FileSystemManager.GetDeletedItems();
        GridView1.DataSource = list;
        GridView1.DataBind();
        lblCurrentPath.Text = FileSystemManager.GetRootPath();
        labpath.Text = "\\回收站";
    }

    private void BindGrid(string path)
    {
        List<FileSystemItem> list = FileSystemManager.GetDeletedItems(path);
        GridView1.DataSource = list;
        GridView1.DataBind();
        lblCurrentPath.Text = path;
        labpath.Text = "\\回收站";
    }
    #endregion
    public void Cancel(object sender, EventArgs e)
    {
        BindGrid(lblCurrentPath.Text);
    }
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            LinkButton lb = (LinkButton)e.Row.Cells[1].FindControl("LinkButton1");
            if (lb.Text != "[根目录]" && lb.Text != "[上一级]")
            {
                if (Directory.Exists(lb.CommandArgument.ToString()))
                {
                    lb.Text = string.Format("<img src=\"images/file/folder.gif\" style=\"border:none; vertical-align:middle;\" /> {0}", lb.Text);
                }
                else
                {
                    string ext = lb.CommandArgument.ToString().Substring(lb.CommandArgument.LastIndexOf(".") + 1);
                    if (lb.CommandArgument.IndexOf(".") != -1 && File.Exists(Server.MapPath(string.Format("images/file/{0}.gif", ext))))
                    {
                        lb.Text = string.Format("<img src=\"images/file/{0}.gif\" style=\"border:none; vertical-align:middle;\" /> {1}", ext, lb.Text);
                    }
                    else
                    {
                        lb.Text = string.Format("<img src=\"images/file/other.gif\" style=\"border:none; vertical-align:middle;\" /> {0}", lb.Text);
                    }
                }
            }
            else
            {
                e.Row.Cells[0].Controls.Clear();
            }
        }
    }

    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (Directory.Exists(e.CommandArgument.ToString()))
        {
            BindGrid(e.CommandArgument.ToString());
        }
        else
        {
            string path = e.CommandArgument.ToString();
            path = path.Replace(FileSystemManager.GetRootPath(), "~");
            path = path.Replace("\\", "/");
            Response.Redirect(path);
        }
    }

    protected void btnDelete_Click(object sender, EventArgs e)
    {
        foreach (GridViewRow row in GridView1.Rows)
        {
            if (row.RowType == DataControlRowType.DataRow)
            {
                CheckBox cb = (CheckBox)row.Cells[0].FindControl("CheckBox1");
                if (cb.Checked)
                {
                    LinkButton lb = (LinkButton)row.Cells[1].FindControl("LinkButton1");
                    if (Directory.Exists(lb.CommandArgument))
                    {
                        FileSystemManager.DeleteFolder(lb.CommandArgument, true);
                    }
                    else
                    {
                        FileSystemManager.DeleteFile(lb.CommandArgument, true);
                    }
                }
            }
        }
        BindGrid(lblCurrentPath.Text);
    }

    protected void btnRecover_Click(object sender, EventArgs e)
    {
        foreach (GridViewRow row in GridView1.Rows)
        {
            if (row.RowType == DataControlRowType.DataRow)
            {
                CheckBox cb = (CheckBox)row.Cells[0].FindControl("CheckBox1");
                if (cb.Checked)
                {
                    LinkButton lb = (LinkButton)row.Cells[1].FindControl("LinkButton1");
                    if (Directory.Exists(lb.CommandArgument))
                    {
                        FileSystemManager.RecoverFolder(lb.CommandArgument);
                    }
                    else
                    {
                        FileSystemManager.RecoverFile(lb.CommandArgument);
                    }
                }
            }
        }
        BindGrid(lblCurrentPath.Text);
    }

}
